﻿// <copyright file="Helper.cs" company="http://www.dymetis.com">
//
// Copyright (c) 2011 Eddie Z. Lin.
//
// This source is subject to the Microsoft Public License.
// See http://www.opensource.org/licenses/ms-pl.
// All other rights reserved.
// 
// </copyright>

namespace Dymetis.TeamAlert
{
    using System;
    using System.Globalization;
    using System.Xml.Linq;
    using System.Xml.Serialization;
    using Microsoft.TeamFoundation.Build.Server;

    internal static class Helper
    {
        public static string Localize(BuildPhaseStatus buildPhaseStatus)
        {
            switch (buildPhaseStatus)
            {
                case BuildPhaseStatus.Unknown:
                    return StringResources.Status_Unknown;

                case BuildPhaseStatus.Failed:
                    return StringResources.Status_Failed;

                case BuildPhaseStatus.Succeeded:
                    return StringResources.Status_Succeeded;
            }

            return String.Empty;
        }

        public static string Localize(BuildStatus status)
        {
            switch (status)
            {
                case BuildStatus.Stopped:
                    return StringResources.Status_Stopped;

                case BuildStatus.NotStarted:
                    return StringResources.Status_NotStarted;

                case BuildStatus.InProgress:
                    return StringResources.Status_InProgress;

                case BuildStatus.Succeeded:
                    return StringResources.Status_SuccessfullyCompleted;

                case BuildStatus.PartiallySucceeded:
                    return StringResources.Status_PartiallySucceeded;

                case BuildStatus.Failed:
                    return StringResources.Status_Failed;
            }

            return String.Empty;
        }

        public static string GetLocalTimeZoneName(this DateTime date)
        {
            var timeZone = TimeZone.CurrentTimeZone;
            return timeZone.IsDaylightSavingTime(date) ? timeZone.DaylightName : timeZone.StandardName;
        }

        public static string GetLocalTimeZoneOffset(this DateTime dateTime)
        {
            var utcOffset = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
            return utcOffset >= TimeSpan.Zero ?
                NumberFormatInfo.CurrentInfo.PositiveSign + utcOffset.ToString() :
                utcOffset.ToString();
        }

        public static XDocument SerializeToXml(this XmlSerializer serializer, object o)
        {
            var eventDocument = new XDocument();

            using (var writer = eventDocument.CreateWriter())
            {
                serializer.Serialize(writer, o);
            }

            return eventDocument;
        }
    }
}
